import requests
import json

header = {'Content-Type': 'application/json'}


def encrypt_rest_api(url, txt, storage, library, table, field):
    data = {"clientId": "read", "storage": storage, "table": table, "library": library,
            "field": field, "datas": []}
    data['datas'].append(txt)
    response = requests.post(url, data=json.dumps(data), headers=header)
    result_info = json.loads(response.content)
    return result_info[0]['body']


def encrypt_string(txt, storage, library, table, field):
    url = 'http://172.19.101.21:9000/encrypt/do_encrypt'
    return encrypt_rest_api(url, txt, storage, library, table, field)


def decrypt_string(txt, storage, library, table, field):
    url = 'http://172.19.101.21:9000/encrypt/do_decrypt'
    return encrypt_rest_api(url, txt, storage, library, table, field)
